package com.apes.scm.masterdata.party.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.party.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 功能：个人客户信息
 *
 * @author HuJianlin
 * @create 2018-03-17 16:35
 */
@Service("personCustomService")
public class PersonCustomService extends DomainService {
    private static final String ROLE_TYPE = "Customer";

    @Autowired
    public PartyService partyService;

    @Autowired
    public PartyRoleService partyRoleService;

    public Party save(SimpleRequest request) {
        Party party = request.getO(Party.class);
        party.setPartyType("person");
        party.addPartyRole(party.getPartyType(), party.getCustomerCode());
        Party result = partyService.saveAndFlush(party, ROLE_TYPE, party.getCustomerCode());

        result.setCustomerCode(party.getCustomerCode());
        return result;
    }

    public Party update(SimpleRequest request){
        // 获取模型
        Party party = request.getO(Party.class);
        party.setUpdateDate(new Date());
        Party result = partyService.saveAndFlush(party, ROLE_TYPE, party.getCustomerCode());
        result.setCustomerCode(party.getCustomerCode());
        return result;
    }

    public Page<Party> findAll(SimpleRequest request) {
        Page<Party> partyPage = partyService.findPartyAllByTypeAndRole(request, "person","Customer");
        return partyPage;
    }

    public String addCustomerRole(SimpleRequest request) {
        return partyRoleService.addPartyTypeAndRoleId(request, "person","Customer");
    }

    public Party findOne(long id) {
        return partyService.findOne(id);
    }

    /**
     * 根据Mobile查找个人客户信息
     * @param request
     * @return
     */
    public Party findByMobile(SimpleRequest request) {
        return partyService.findByMobile(request);
    }

    /**
     * 根据Code查找个人客户信息
     * @param request
     * @return
     */
    public Party findByCode(SimpleRequest request) {
        return partyService.findByCode(request, ROLE_TYPE + "Code");
    }

}
